package org.goobs.gui;

import java.util.Iterator;
import java.util.LinkedList;

public class Grid{
	
	private boolean buildRows = false;
	private boolean buildCols = false;
	
	private int[] rows;
	private int[] cols;
	
	private final LinkedList <Integer> rowList = new LinkedList <Integer> ();
	private final LinkedList <Integer> colList = new LinkedList <Integer> ();
	
	public Grid(){
		
	}
	public Grid(int[] heights, int[] widths){
		this.rows = heights;
		for(int height : heights){
			rowList.addLast(height);
		}
		this.cols = widths;
		for(int width : widths){
			this.colList.addLast(width);
		}
	}
	
	
	
	public void addRow(int height){
		rowList.addLast(height);
		buildRows = true;
	}
	
	public void insertRow(int pos, int height){
		rowList.add(pos, height);
		buildRows = true;
	}
	
	public void removeRow(int pos){
		rowList.remove(pos);
		buildRows = true;
	}
	
	public void addCol(int height){
		colList.addLast(height);
		buildCols = true;
	}
	
	public void insertCol(int pos, int height){
		colList.add(pos, height);
		buildCols = true;
	}
	
	public void removeCol(int pos){
		colList.remove(pos);
		buildCols = true;
	}
	
	
	
	public int getHeight(int rowIndex){
		if(buildRows){
			buildRows();
		}
		return rows[rowIndex];
	}
	
	public int getWidth(int colIndex){
		if(buildCols){
			buildCols();
		}
		return rows[colIndex];
	}
	
	public int numRows(){
		return rowList.size();
	}
	
	public int numCols(){
		return colList.size();
	}
	
	
	private void buildRows(){
		rows = new int[rowList.size()];
		int i=0;
		Iterator <Integer> iter = rowList.iterator();
		while(iter.hasNext()){
			rows[i] = iter.next();
			i++;
		}
		buildRows = false;
	}
	
	private void buildCols(){
		cols = new int[colList.size()];
		int i=0;
		Iterator <Integer> iter = colList.iterator();
		while(iter.hasNext()){
			cols[i] = iter.next();
			i++;
		}
		buildCols = false;
	}
	
}
